package com.run.guifeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.run.guifeng.dao.ShoppingCartItemMapper;
import com.run.guifeng.model.dto.ShoppingCartDTO;
import com.run.guifeng.model.entity.GoodsInfo;
import com.run.guifeng.model.entity.ShoppingCartItem;
import com.run.guifeng.model.entity.User;
import com.run.guifeng.model.vo.ShoppingCartItemVO;
import com.run.guifeng.service.GoodsService;
import com.run.guifeng.service.ShoppingCartService;
import com.run.guifeng.service.UserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartItemMapper shoppingCartItemMapper;
    @Resource
    private UserService userService;
    @Autowired
    private GoodsService goodsService;
    @Override
    public boolean saveCartItem(ShoppingCartItem shoppingCartItem) {
        //TODO
        Integer goodsId = shoppingCartItem.getGoodsId();
        Integer userId = shoppingCartItem.getUserId();
        GoodsInfo goods = goodsService.getMallGoodsById(goodsId);
        User user = userService.searchUserById(userId);
        if(user!=null && goods!=null){
            int count = shoppingCartItemMapper.insert(shoppingCartItem);
            if(count==1)
                return true;
        }
        return false;
    }

    @Override
    public void updateCartItem(ShoppingCartItem shoppingCartItem) {
        shoppingCartItemMapper.selectOne(
                new UpdateWrapper<ShoppingCartItem>()
                      .set("goods_count",shoppingCartItem.getGoodsCount())
                        .eq("id",shoppingCartItem.getId())
        );
    }

    @Override
    public ShoppingCartItem getCartItemById(Long shoppingCartItemId) {
        ShoppingCartItem item = shoppingCartItemMapper.selectOne(
                new QueryWrapper<ShoppingCartItem>()
                        .eq("id", shoppingCartItemId)
        );
        return item;
    }

    @Override
    public Boolean deleteById(Long shoppingCartItemId) {
        int row = shoppingCartItemMapper.deleteById(shoppingCartItemId);
        if(row==1)
            return true;
        return false;
    }

    @Override
    public List<ShoppingCartDTO> getMyShoppingCartItems(Long userId) {

        List<ShoppingCartItem> items = shoppingCartItemMapper.selectList(
                new QueryWrapper<ShoppingCartItem>()
                        .eq("user_id", userId)
        );
        return returnShoppingCartDTO(items);
    }

    @Override
    public void batchDeleteShopCartItem(Integer userid, List<Integer> goodsIds){
        for(Integer goodsId : goodsIds){
            shoppingCartItemMapper.delete(
                    new QueryWrapper<ShoppingCartItem>()
                    .eq("user_id",userid)
                    .eq("goods_id",goodsId)
            );
        }
    }

    private List<ShoppingCartDTO> returnShoppingCartDTO(List<ShoppingCartItem> items) {
        ShoppingCartItemVO shoppingCartItemVO = new ShoppingCartItemVO();
        String goodsName="";
        String goodsCoverImg = "";
        String goodsIntro="";
        double price = 0;
        List<ShoppingCartDTO> list = new ArrayList<>();
        for (ShoppingCartItem shoppingCartItem:items) {
            ShoppingCartDTO shoppingCart = new ShoppingCartDTO();
            GoodsInfo goodsInfo = goodsService.getMallGoodsById(shoppingCartItem.getGoodsId());
            goodsName = goodsInfo.getGoodsName();
            goodsCoverImg = goodsInfo.getGoodsCoverImg();
            price = goodsInfo.getSellingPrice();
            goodsIntro = goodsInfo.getGoodsIntro();
            shoppingCart.setCartItemId(shoppingCartItem.getId());
            shoppingCart.setGoodsCount(shoppingCartItem.getGoodsCount());
            shoppingCart.setGoodsCoverImg(goodsCoverImg);
            shoppingCart.setGoodsName(goodsName);
            shoppingCart.setSellingPrice(price);
            shoppingCart.setGoodsDesc(goodsIntro);
            shoppingCart.setGoodsId(shoppingCartItem.getGoodsId());
            list.add(shoppingCart);
        }
        return list;
    }
}
